﻿
CREATE PROCEDURE dbo.SpGetDefaultParameters
(
	@UserId VARCHAR(50),
	@ReportServerUrl VARCHAR(128),
	@ReportName VARCHAR(128)
)
AS

IF (SELECT ConfigValue FROM dbo.Configuration WHERE ConfigType = 'HashUserId') = 1
	SET @UserId = HashBytes('SHA1', @UserId)
	
DECLARE @ParameterValue VARCHAR(MAX)
SET @ParameterValue = ''

DECLARE @ROWCOUNT INT

DECLARE @LatestExecutionId INT

--Get the latest execution of the report for the particular user
SELECT TOP(1) @LatestExecutionId = ExecutionId
FROM dbo.DefaultReportParameterExecution Pe
WHERE
	Pe.UserId = @UserId 
	AND Pe.ReportName = @ReportName
	AND Pe.ReportServerUrl = @ReportServerUrl
ORDER BY ExecutionTime DESC

--Check first if the information should be tracked at all
IF NOT (SELECT ConfigValue FROM dbo.Configuration WHERE ConfigType = 'TrackParameterValues') = 1
	RETURN 
	
SELECT Parametername, ParameterValue
FROM dbo.DefaultReportParameters P
WHERE
	P.ExecutionId = @LatestExecutionId

RETURN 

